package kr.hkit.Eastjo_FinalProject.Menu;

import kr.hkit.Eastjo_FinalProject.*;
import kr.hkit.Eastjo_FinalProject.CardAni.TransitionUtil;
import kr.hkit.Eastjo_FinalProject.Menu.C_food;
import kr.hkit.Eastjo_FinalProject.Menu.ETC_food;
import kr.hkit.Eastjo_FinalProject.Menu.F_food;
import kr.hkit.Eastjo_FinalProject.Menu.J_food;
import kr.hkit.Eastjo_FinalProject.Menu.K_food;
import kr.hkit.eastjo_app_proj.*;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewDebug.FlagToString;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.widget.EditText;
import android.widget.PopupMenu;
import android.widget.PopupWindow;
import android.widget.Toast;
import android.widget.ViewFlipper;
import android.widget.ViewSwitcher;

public class ETC_food extends Activity {
	ViewFlipper mFlip;
	// Button mBtn;
	private PopupWindow mPopupWindow;
	private BackPressCloseHandler backPressCloseHandler; // 백키 두번 종료
	// 온플링
	final static int DISTANCE = 100; // 거리
	final static int VELOCITY = 10; // 속도
	GestureDetector mDetector;

	private ViewSwitcher mSwitcher13, mSwitcher14, mSwitcher15, mSwitcher16;

	private EditText et;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Log.e("eastjo", "메인시작 전");
		setContentView(R.layout.e_food);
		 backPressCloseHandler = new BackPressCloseHandler(this);
		mFlip = (ViewFlipper) findViewById(R.id.flip);
		// mBtn = (Button) findViewById(R.id.btnmenu);

		// 온플링
		mSwitcher13 = (ViewSwitcher) findViewById(R.id.card401);
		mSwitcher14 = (ViewSwitcher) findViewById(R.id.card402);
		mSwitcher15 = (ViewSwitcher) findViewById(R.id.card403);
		mSwitcher16 = (ViewSwitcher) findViewById(R.id.card404); // 기타

		mSwitcher13.setOnClickListener(onRotate);
		mSwitcher14.setOnClickListener(onRotate);
		mSwitcher15.setOnClickListener(onRotate);
		mSwitcher16.setOnClickListener(onRotate);

		mDetector = new GestureDetector(this, mGestureListener);
		mDetector.setIsLongpressEnabled(false);

	}

	// 메모하기
	private AlertDialog create_inputDialog(final String key) {
		AlertDialog dialogBox = new AlertDialog.Builder(this)
				.setTitle("My Tip")
				// .setMessage("값을 입력하세요")
				.setPositiveButton("확인", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						// Toast.makeText(getApplicationContext(), et.getText(),
						// Toast.LENGTH_SHORT).show();

						// Toast.makeText(AlertDialog.this, "메모선택.",
						// Toast.LENGTH_SHORT).show();
						saveMemoPreferences(et.getText().toString(), key);
					}
				}).create();
		return dialogBox;
	}

	public void memoClick(View v) {
		Log.e("memoClick", "메모클릭");
		switch (v.getId()) {
		case R.id.memobtn401:
			insertMemo("401");
			break;
		case R.id.memobtn402:
			insertMemo("402");
			break;
		case R.id.memobtn403:
			insertMemo("403");
			break;
		case R.id.memobtn404:
			insertMemo("404");
			break;
		}
	}

	private void insertMemo(String key) {
		Toast.makeText(this, "메모선택.", Toast.LENGTH_SHORT).show();
		// intent = new Intent(this, Memo.class);
		AlertDialog dialog = create_inputDialog(key);
		// Context 얻고, 해당 컨텍스트의 레이아웃 정보 얻기
		Context context = getApplicationContext();
		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(LAYOUT_INFLATER_SERVICE);
		// 레이아웃 설정
		View layout = inflater.inflate(R.layout.alertdialog,
				(ViewGroup) findViewById(R.id.edittextView));
		et = (EditText) layout.findViewById(R.id.edittextView);
		et.setSelection(0);
		et.setText(getMemoPreferences(key));
		// Input 소프트 키보드 보이기
		//dialog.getWindow().setSoftInputMode(
			//	WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
		// AlertDialog에 레이아웃 추가
		dialog.setView(layout);
		dialog.show();
	}

	public boolean onTouchEvent(MotionEvent event) {
		return mDetector.onTouchEvent(event);
	}

	// 카드넘기기 온플링/뷰플리퍼
	OnGestureListener mGestureListener = new OnGestureListener() {
		@Override
		public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
				float velocityY) {
			if (Math.abs(velocityX) > VELOCITY) {
				if (e2.getX() - e1.getX() > DISTANCE) {
					mFlip.showPrevious();

				}
				if (e1.getX() - e2.getX() > DISTANCE) {
					mFlip.showNext();
				}
			}
			return true;
		}

		public boolean onDown(MotionEvent e) {
			return false;
		}

		public void onLongPress(MotionEvent e) {
		}

		public boolean onScroll(MotionEvent e1, MotionEvent e2,
				float distanceX, float distanceY) {
			return false;
		}

		public void onShowPress(MotionEvent e) {
		}

		public boolean onSingleTapUp(MotionEvent e) {
			return false;
		}

	};

	// 메뉴
	public void mOnClick(View v) {

		PopupMenu popup = new PopupMenu(this, v);
		MenuInflater inflater = popup.getMenuInflater();
		Menu menu = popup.getMenu();
		inflater.inflate(R.menu.popupmenulist, menu);

		popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
			public boolean onMenuItemClick(MenuItem item) {
				Intent intent = null;
				Log.e("eastjo", "팝업메뉴");
				switch (item.getItemId()) {

				case R.id.All:
					Toast.makeText(ETC_food.this, "전체메뉴", 0).show();
					intent = new Intent(ETC_food.this, MainActivity.class);
					break;
				case R.id.Fevorites:
					Toast.makeText(ETC_food.this, "즐겨찾기", 0).show();
					intent = new Intent(ETC_food.this, F_food.class);
					break;
				case R.id.K_food:
					Toast.makeText(ETC_food.this, "한식 선택", 0).show();
					intent = new Intent(ETC_food.this, K_food.class);

					break;
				case R.id.C_food:
					Toast.makeText(ETC_food.this, "중식 선택", 0).show();
					intent = new Intent(ETC_food.this, C_food.class);

					break;
				case R.id.J_food:
					Toast.makeText(ETC_food.this, "일식 선택", 0).show();
					intent = new Intent(ETC_food.this, J_food.class);

					break;
				case R.id.ETC:
					Toast.makeText(ETC_food.this, "기타메뉴 선택", 0).show();
					intent = new Intent(ETC_food.this, ETC_food.class);

					break;
				}
				startActivity(intent);
				//intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
				finish();
				return true;
			}
		});
		popup.show();

	}

	// 카드 뒤집기
	private OnClickListener onRotate = new OnClickListener() {
		@Override
		public void onClick(View v) {

			Log.e("dwpark", "카드 뒤집기");

			TransitionUtil.flipTransition(mSwitcher13,
					TransitionUtil.FlipDirection.LEFT_RIGHT, 0.0f);

			TransitionUtil.flipTransition(mSwitcher14,
					TransitionUtil.FlipDirection.LEFT_RIGHT, 0.0f);

			TransitionUtil.flipTransition(mSwitcher15,
					TransitionUtil.FlipDirection.LEFT_RIGHT, 0.0f);

			TransitionUtil.flipTransition(mSwitcher16,
					TransitionUtil.FlipDirection.LEFT_RIGHT, 0.0f);

			// Set the views here when outanimation is started.
			mSwitcher13.getOutAnimation().setAnimationListener(
					new AnimationListener() {
						public void onAnimationStart(Animation animation) {
						}

						public void onAnimationRepeat(Animation animation) {
						}

						public void onAnimationEnd(Animation animation) {
						}
					});
			// Set the views here when inanimation is ended.
			mSwitcher13.getInAnimation().setAnimationListener(
					new AnimationListener() {
						public void onAnimationStart(Animation animation) {
						}

						public void onAnimationRepeat(Animation animation) {
						}

						public void onAnimationEnd(Animation animation) {
						}
					});
		}
	};

	// 값 저장하기
	private void saveMemoPreferences(String memo, String key) {
		SharedPreferences pref = getSharedPreferences("Memo",
				Activity.MODE_PRIVATE);
		SharedPreferences.Editor editor = pref.edit();
		editor.putString(key, memo);
		editor.commit();
	}

	// 값 불러오기
	private String getMemoPreferences(String key) {
		SharedPreferences pref = getSharedPreferences("Memo",
				Activity.MODE_PRIVATE);
		String str = pref.getString(key, "");
		return str;
	}
	// 백키두번 종료
    @Override
    public void onBackPressed() {
        //super.onBackPressed();
        backPressCloseHandler.onBackPressed();
    }
}
